package com.example.database.slice;

import com.example.database.ResourceTable;
import com.example.database.Utils;
import com.example.database.orm.Picture;
import com.example.database.orm.TestDatabase;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;
import ohos.data.rdb.RdbPredicates;
import ohos.data.rdb.RdbStore;
import ohos.data.rdb.ValuesBucket;

import java.util.Date;
import java.util.List;

public class OrmAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private Button mBtnOrmInsert;
    private Button mBtnOrmQuery;
    private Button mBtnOrmUpdate;
    private Button mBtnOrmDelete;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_orm);
        mBtnOrmInsert = (Button) findComponentById(ResourceTable.Id_btn_orm_insert);
        mBtnOrmInsert.setClickedListener(this);
        mBtnOrmQuery = (Button) findComponentById(ResourceTable.Id_btn_orm_query);
        mBtnOrmQuery.setClickedListener(this);
        mBtnOrmUpdate = (Button) findComponentById(ResourceTable.Id_btn_orm_update);
        mBtnOrmUpdate.setClickedListener(this);
        mBtnOrmDelete = (Button) findComponentById(ResourceTable.Id_btn_orm_delete);
        mBtnOrmDelete.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {

        if(component.getId() == ResourceTable.Id_btn_orm_insert) {
            // 获取OrmContext对象
            OrmContext context = getTestOrmContext();

            // 创建ORM对象
            Picture picture = new Picture();
            picture.setName("test.png");
            picture.setCapturedTime(new Date());
            picture.setDiscription("测试照片");
            picture.setLongitude(116.445088);
            picture.setLatitude(39.942821);
            // 插入ORM对象
            boolean isSuccessed = context.insert(picture);
            if (!isSuccessed) {
                Utils.showToast(this, "增加数据错误!");
                return;
            }
            // 提交操作
            isSuccessed = context.flush();
            if (!isSuccessed) {
                Utils.showToast(this, "增加数据错误!");
                return;
            }
            Utils.showToast(this, "增加数据成功!");
            // 使用完OrmContext对象后要及时关闭
            context.close();
        }

        if(component.getId() == ResourceTable.Id_btn_orm_query) {
            // 获取OrmContext对象
            OrmContext context = getTestOrmContext();

            // 创建OrmPredicates对象，指定查询条件
            OrmPredicates predicates = new OrmPredicates(Picture.class)
                    .orderByAsc("pictureid");
            // 查询并获得结果列表
            List<Picture> pictures = context.query(predicates);
            // 打印查询结果
            Utils.showToast(this, "查询结果数：" + pictures.size());
            for (Picture picture : pictures) {
                Utils.log("名称:" + picture.getName()
                        + " 时间:" + picture.getCapturedTime()
                        + " 描述: " + picture.getDiscription());
            }
            context.close();
        }

        if(component.getId() == ResourceTable.Id_btn_orm_update) {

            // 获取OrmContext对象
            OrmContext context = getTestOrmContext();

            // 创建OrmPredicates对象，指定更新条件
            OrmPredicates ormPredicates = new OrmPredicates(Picture.class)
                    .equalTo("name", "test.png");
            // 通过ValuesBucket对象指定更新字段值
            ValuesBucket values = new ValuesBucket();
            values.putString("name", "test1.png");
            // 更新数据
            int count = context.update(ormPredicates, values);
            boolean isSuccessed = context.flush();
            Utils.showToast(this, "更新是否成功:" + isSuccessed + " 更新记录数:" + count);
            context.close();

        }
        if(component.getId() == ResourceTable.Id_btn_orm_delete) {

            // 获取OrmContext对象
            OrmContext context = getTestOrmContext();

            // Row ID为1的Picture对象
            Picture picture = new Picture();
            picture.setRowId(1);
            // 删除数据
            boolean isSuccessed = context.delete(picture);
            if (!isSuccessed) {
                Utils.showToast(this, "删除数据错误!");
                return;
            }
            // 提交操作
            isSuccessed = context.flush();
            if (!isSuccessed) {
                Utils.showToast(this, "删除数据错误!");
                return;
            }
            Utils.showToast(this, "删除数据成功!");
            context.close();
        }
    }


    /**
     * 获取测试数据库test.db的OrmContext对象
     * @return OrmContext对象
     */
    private OrmContext getTestOrmContext() {
        DatabaseHelper helper = new DatabaseHelper(this);
        OrmContext context = helper.getOrmContext("Test", "test.db", TestDatabase.class);
        return context;
    }
}
